﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Top.Core;
using Top.Data;

namespace Top.PointStore.SyncDataBaseService
{
    public static class DataBaseSyncManager
    {
        public static void SyncDataCount_BrokerCompany()
        {
            //先同步数据量
            string sql = @"SELECT COUNT(0) FROM t_brokercompany";
            using (var db = DataBaseContext.GetDataBase("default"))
            {
                List<T_BrokerCompany> newList = new List<T_BrokerCompany>();
                var count_MySql = db.Sql(sql).ExecuteScalar().Result;
                using (var db2 = DataBaseContext.GetDataBase("sqldefault"))
                {
                    var count_SqlServer = db2.Sql(sql).ExecuteScalar().Result;
                    int countM = Convert3.To<int>(count_MySql);
                    int countS = Convert3.To<int>(count_SqlServer);
                    if (countS > countM)
                    {
                        string sql2 = @"select max(kid) from t_brokercompany";
                        int maxId_MySql = Convert3.To<int>(db.Sql(sql2).ExecuteScalar().Result);
                        string sql3 = @"select * from t_brokercompany where kid>{0}";
                        newList = db2.Sql(sql3, maxId_MySql).ToList<T_BrokerCompany>().Result;
                    }
                    #region 新增经济公司
                    string sql4 = @"
INSERT INTO T_BrokerCompany
           (Kid
           ,F_ParentKid
           ,F_ParentPath
           ,F_TopKid
           ,F_Level
           ,F_IsChild
           ,F_ProvinceKid
           ,F_CityKid
           ,F_AreaKid
           ,F_Area
           ,F_Code
           ,F_Title
           ,F_ShortName
           ,F_Address
           ,F_Longitude
           ,F_Latitude
           ,F_BankType
           ,F_Bank
           ,F_IsLeague
           ,F_IsLeague_9
           ,F_ManCount
           ,F_UseCount
           ,F_IsHidePhone
           ,F_HideRule
           ,F_IsCom
           ,F_ComValue
           ,F_Remark
           ,F_Enable
           ,F_Sort
           ,F_AddTime
           ,F_AddAdminKid
           ,F_EditTime
           ,F_EditAdminKid
           ,F_DirectorAdminKid
           ,F_IsDistribute
           ,F_CompanyMoneyGetRatio
           ,F_IsFirstCheck)
     VALUES
           ({0:Kid}
           ,{0:F_ParentKid}
           ,{0:F_ParentPath}
           ,{0:F_TopKid}
           ,{0:F_Level}
           ,{0:F_IsChild}
           ,{0:F_ProvinceKid}
           ,{0:F_CityKid}
           ,{0:F_AreaKid}
           ,{0:F_Area}
           ,{0:F_Code}
           ,{0:F_Title}
           ,{0:F_ShortName}
           ,{0:F_Address}
           ,{0:F_Longitude}
           ,{0:F_Latitude}
           ,{0:F_BankType}
           ,{0:F_Bank}
           ,{0:F_IsLeague}
           ,{0:F_IsLeague_9}
           ,{0:F_ManCount}
           ,{0:F_UseCount}
           ,{0:F_IsHidePhone}
           ,{0:F_HideRule}
           ,{0:F_IsCom}
           ,{0:F_ComValue}
           ,{0:F_Remark}
           ,{0:F_Enable}
           ,{0:F_Sort}
           ,{0:F_AddTime}
           ,{0:F_AddAdminKid}
           ,{0:F_EditTime}
           ,{0:F_EditAdminKid}
           ,{0:F_DirectorAdminKid}
           ,{0:F_IsDistribute}
           ,{0:F_CompanyMoneyGetRatio}
           ,{0:F_IsFirstCheck})
";
                    #endregion
                    for (int i = 0; i < newList.Count; i++)
                    {
                        db.Sql(sql4, newList[i]).ExecuteNonQuery();
                    }
                }
            }
        }

        public static void SyncDataValue_BrokerCompany()
        {
            string sql = @"SELECT * FROM t_brokercompany";
            List<T_BrokerCompany> mySqlList = new List<T_BrokerCompany>();
            List<T_BrokerCompany> sqlServerList = new List<T_BrokerCompany>();
            List<T_BrokerCompany> updateList = new List<T_BrokerCompany>();
            using (var db = DataBaseContext.GetDataBase("default"))
            {
                mySqlList = db.Sql(sql).ToList<T_BrokerCompany>().Result;
            }
            using (var db2 = DataBaseContext.GetDataBase("sqldefault"))
            {
                sqlServerList = db2.Sql(sql).ToList<T_BrokerCompany>().Result;
            }
            if (mySqlList.Count != sqlServerList.Count)
            {
                return;
            }
            for (int i = 0; i < sqlServerList.Count; i++)
            {
                if (!sqlServerList[i].Equals(mySqlList[i]))
                {
                    updateList.Add(sqlServerList[i]);
                }
            }

            if (updateList.Count > 0)
            {
                #region 更新经纪人公司数据
                string sql2 = @"
UPDATE T_BrokerCompany
   SET F_ParentKid = {0:F_ParentKid}
      ,F_ParentPath = {0:F_ParentPath}
      ,F_TopKid = {0:F_TopKid}
      ,F_Level = {0:F_Level}
      ,F_IsChild = {0:F_IsChild}
      ,F_ProvinceKid = {0:F_ProvinceKid}
      ,F_CityKid = {0:F_CityKid}
      ,F_AreaKid = {0:F_AreaKid}
      ,F_Area = {0:F_Area}
      ,F_Code = {0:F_Code}
      ,F_Title = {0:F_Title}
      ,F_ShortName = {0:F_ShortName}
      ,F_Address = {0:F_Address}
      ,F_Longitude = {0:F_Longitude}
      ,F_Latitude = {0:F_Latitude}
      ,F_BankType = {0:F_BankType}
      ,F_Bank = {0:F_Bank}
      ,F_IsLeague = {0:F_IsLeague}
      ,F_IsLeague_9 = {0:F_IsLeague_9}
      ,F_ManCount = {0:F_ManCount}
      ,F_UseCount = {0:F_UseCount}
      ,F_IsHidePhone = {0:F_IsHidePhone}
      ,F_HideRule = {0:F_HideRule}
      ,F_IsCom = {0:F_IsCom}
      ,F_ComValue = {0:F_ComValue}
      ,F_Remark = {0:F_Remark}
      ,F_Enable = {0:F_Enable}
      ,F_Sort = {0:F_Sort}
      ,F_AddTime = {0:F_AddTime}
      ,F_AddAdminKid = {0:F_AddAdminKid}
      ,F_EditTime = {0:F_EditTime}
      ,F_EditAdminKid = {0:F_EditAdminKid}
      ,F_DirectorAdminKid = {0:F_DirectorAdminKid}
      ,F_IsDistribute = {0:F_IsDistribute}
      ,F_CompanyMoneyGetRatio = {0:F_CompanyMoneyGetRatio}
      ,F_IsFirstCheck = {0:F_IsFirstCheck}
 WHERE Kid = {0:Kid}
";
                #endregion
                using (var db3 = DataBaseContext.GetDataBase("default"))
                {
                    for (int i = 0; i < updateList.Count; i++)
                    {
                        db3.Sql(sql2, updateList[i]).ExecuteNonQuery();
                    }
                }
            }
        }

        public static void SyncDataValue_BrokerCompnay_Part()
        {
            string sql = @"SELECT F_CityKid,F_AreaKid,F_Area,F_Title,F_Address,F_IsDistribute,F_EditTime FROM t_brokercompany where F_EditTime>{0}";
            List<T_BrokerCompany> mySqlList = new List<T_BrokerCompany>();
            List<T_BrokerCompany> sqlServerList = new List<T_BrokerCompany>();
            List<T_BrokerCompany> updateList = new List<T_BrokerCompany>();
            using (var db2 = DataBaseContext.GetDataBase("sqldefault"))
            {
                sqlServerList = db2.Sql(sql, DateTime.Now.Date.AddDays(-1)).ToList<T_BrokerCompany>().Result;
            }
            if (sqlServerList.Count == 0)
            {
                //没有改变不用更新
                return;
            }
            for (int i = 0; i < sqlServerList.Count; i++)
            {
                updateList.Add(sqlServerList[i]);
            }
            if (updateList.Count > 0)
            {
                #region 更新经纪人公司数据
                string sql2 = @"
UPDATE T_BrokerCompany
   SET F_ParentKid = {0:F_ParentKid}
      ,F_ParentPath = {0:F_ParentPath}
      ,F_TopKid = {0:F_TopKid}
      ,F_Level = {0:F_Level}
      ,F_IsChild = {0:F_IsChild}
      ,F_ProvinceKid = {0:F_ProvinceKid}
      ,F_CityKid = {0:F_CityKid}
      ,F_AreaKid = {0:F_AreaKid}
      ,F_Area = {0:F_Area}
      ,F_Code = {0:F_Code}
      ,F_Title = {0:F_Title}
      ,F_ShortName = {0:F_ShortName}
      ,F_Address = {0:F_Address}
      ,F_Longitude = {0:F_Longitude}
      ,F_Latitude = {0:F_Latitude}
      ,F_BankType = {0:F_BankType}
      ,F_Bank = {0:F_Bank}
      ,F_IsLeague = {0:F_IsLeague}
      ,F_IsLeague_9 = {0:F_IsLeague_9}
      ,F_ManCount = {0:F_ManCount}
      ,F_UseCount = {0:F_UseCount}
      ,F_IsHidePhone = {0:F_IsHidePhone}
      ,F_HideRule = {0:F_HideRule}
      ,F_IsCom = {0:F_IsCom}
      ,F_ComValue = {0:F_ComValue}
      ,F_Remark = {0:F_Remark}
      ,F_Enable = {0:F_Enable}
      ,F_Sort = {0:F_Sort}
      ,F_AddTime = {0:F_AddTime}
      ,F_AddAdminKid = {0:F_AddAdminKid}
      ,F_EditTime = {0:F_EditTime}
      ,F_EditAdminKid = {0:F_EditAdminKid}
      ,F_DirectorAdminKid = {0:F_DirectorAdminKid}
      ,F_IsDistribute = {0:F_IsDistribute}
      ,F_CompanyMoneyGetRatio = {0:F_CompanyMoneyGetRatio}
      ,F_IsFirstCheck = {0:F_IsFirstCheck}
 WHERE Kid = {0:Kid}
";
                #endregion
                using (var db3 = DataBaseContext.GetDataBase("default"))
                {
                    for (int i = 0; i < updateList.Count; i++)
                    {
                        db3.Sql(sql2, updateList[i]).ExecuteNonQuery();
                    }
                }
            }
        }

        public static void SyncDataCount_Broker()
        {
            //先同步数据量
            string sql = @"SELECT COUNT(0) FROM t_broker";
            using (var db = DataBaseContext.GetDataBase("default"))
            {
                List<T_Broker> newList = new List<T_Broker>();
                var count_MySql = db.Sql(sql).ExecuteScalar().Result;
                using (var db2 = DataBaseContext.GetDataBase("sqldefault"))
                {
                    var count_SqlServer = db2.Sql(sql).ExecuteScalar().Result;
                    int countM = Convert3.To<int>(count_MySql);
                    int countS = Convert3.To<int>(count_SqlServer);
                    if (countS > countM)
                    {
                        string sql2 = @"select max(kid) from t_broker";
                        int maxId_MySql = Convert3.To<int>(db.Sql(sql2).ExecuteScalar().Result);
                        string sql3 = @"select * from t_broker where kid>{0}";
                        newList = db2.Sql(sql3, maxId_MySql).ToList<T_Broker>().Result;
                    }
                    #region 插入Borker表
                    string sql4 = @"
INSERT INTO T_Broker
           (Kid
           ,F_ProvinceKid
           ,F_CityKid
           ,F_LocationCityKid
           ,F_Title
           ,F_Phone
           ,F_PassWord
           ,F_IDNumber
           ,F_AddTime
           ,F_EditTime
           ,F_WeiXinID
           ,F_PicUrl
           ,F_Status
           ,F_AuthTime
           ,F_PassTime
           ,F_RealTime
           ,F_LastActiveTime
           ,F_Bank
           ,F_BankAccount
           ,F_RecommendPhone
           ,F_WX_Nickname
           ,F_WX_Country
           ,F_WX_Province
           ,F_WX_City
           ,F_WX_Language
           ,F_WX_Sex
           ,F_WX_Subscribe
           ,F_WX_Subscribe_time
           ,F_Type
           ,F_BrokerCompanyName
           ,F_BrokerCompanyKid
           ,F_Longitude
           ,F_Latitude
           ,F_LastLocationTime
           ,F_FirstAgreementTime
           ,F_LastAgreementTime
           ,F_BusinessCardUrl
           ,F_TagIsSuperAuth
           ,F_TagIsSuperAuthTime
           ,F_TagYear
           ,F_TagProgramCount
           ,F_TagBusinessCount
           ,F_TagSpareAvgMoney
           ,F_TagCommentCount
           ,F_Tag
           ,F_Remark
           ,F_CheckInTime
           ,F_QrCode
           ,F_Ex
           ,F_DeviceID
           ,F_AppVersion
           ,F_HxUuid
           ,F_CardUrl
           ,F_CardCheckAdminKid
           ,F_CardCheckTime
           ,AnyangKid
           ,F_CardCheckStatus
           ,F_StarStatus
)
     VALUES
           ({0:Kid}
		   ,{0:F_ProvinceKid}
           ,{0:F_CityKid}
           ,{0:F_LocationCityKid}
           ,{0:F_Title}
           ,{0:F_Phone}
           ,{0:F_PassWord}
           ,{0:F_IDNumber}
           ,{0:F_AddTime}
           ,{0:F_EditTime}
           ,{0:F_WeiXinID}
           ,{0:F_PicUrl}
           ,{0:F_Status}
           ,{0:F_AuthTime}
           ,{0:F_PassTime}
           ,{0:F_RealTime}
           ,{0:F_LastActiveTime}
           ,{0:F_Bank}
           ,{0:F_BankAccount}
           ,{0:F_RecommendPhone}
           ,{0:F_WX_Nickname}
           ,{0:F_WX_Country}
           ,{0:F_WX_Province}
           ,{0:F_WX_City}
           ,{0:F_WX_Language}
           ,{0:F_WX_Sex}
           ,{0:F_WX_Subscribe}
           ,{0:F_WX_Subscribe_time}
           ,{0:F_Type}
           ,{0:F_BrokerCompanyName}
           ,{0:F_BrokerCompanyKid}
           ,{0:F_Longitude}
           ,{0:F_Latitude}
           ,{0:F_LastLocationTime}
           ,{0:F_FirstAgreementTime}
           ,{0:F_LastAgreementTime}
           ,{0:F_BusinessCardUrl}
           ,{0:F_TagIsSuperAuth}
           ,{0:F_TagIsSuperAuthTime}
           ,{0:F_TagYear}
           ,{0:F_TagProgramCount}
           ,{0:F_TagBusinessCount}
           ,{0:F_TagSpareAvgMoney}
           ,{0:F_TagCommentCount}
           ,{0:F_Tag}
           ,{0:F_Remark}
           ,{0:F_CheckInTime}
           ,{0:F_QrCode}
           ,{0:F_Ex}
           ,{0:F_DeviceID}
           ,{0:F_AppVersion}
           ,{0:F_HxUuid}
           ,{0:F_CardUrl}
           ,{0:F_CardCheckAdminKid}
           ,{0:F_CardCheckTime}
           ,{0:AnyangKid}
           ,{0:F_CardCheckStatus}
           ,{0:F_StarStatus})
";
                    #endregion
                    for (int i = 0; i < newList.Count; i++)
                    {
                        db.Sql(sql4, newList[i]).ExecuteNonQuery();
                    }
                }
            }
        }

        public static void SyncDataValue_Broker()
        {
            string sql = @"SELECT * FROM t_broker";
            List<T_Broker> mySqlList = new List<T_Broker>();
            List<T_Broker> sqlServerList = new List<T_Broker>();
            List<T_Broker> updateList = new List<T_Broker>();
            using (var db = DataBaseContext.GetDataBase("default"))
            {
                mySqlList = db.Sql(sql).ToList<T_Broker>().Result;
            }
            using (var db2 = DataBaseContext.GetDataBase("sqldefault"))
            {
                sqlServerList = db2.Sql(sql).ToList<T_Broker>().Result;
            }
            if (mySqlList.Count != sqlServerList.Count)
            {
                return;
            }
            for (int i = 0; i < sqlServerList.Count; i++)
            {
                if (!sqlServerList[i].Equals(mySqlList[i]))
                {
                    updateList.Add(sqlServerList[i]);
                }
            }

            if (updateList.Count > 0)
            {
                #region 更新经纪人数据
                string sql2 = @"
UPDATE T_Broker
   SET F_ProvinceKid = {0:F_ProvinceKid}
      ,F_CityKid = {0:F_CityKid}
      ,F_LocationCityKid = {0:F_LocationCityKid}
      ,F_Title = {0:F_Title}
      ,F_Phone = {0:F_Phone}
      ,F_PassWord = {0:F_PassWord}
      ,F_IDNumber = {0:F_IDNumber}
      ,F_AddTime = {0:F_AddTime}
      ,F_EditTime = {0:F_EditTime}
      ,F_WeiXinID = {0:F_WeiXinID}
      ,F_PicUrl = {0:F_PicUrl}
      ,F_Status = {0:F_Status}
      ,F_AuthTime = {0:F_AuthTime}
      ,F_PassTime = {0:F_PassTime}
      ,F_RealTime = {0:F_RealTime}
      ,F_LastActiveTime = {0:F_LastActiveTime}
      ,F_Bank = {0:F_Bank}
      ,F_BankAccount = {0:F_BankAccount}
      ,F_RecommendPhone = {0:F_RecommendPhone}
      ,F_WX_Nickname = {0:F_WX_Nickname}
      ,F_WX_Country = {0:F_WX_Country}
      ,F_WX_Province = {0:F_WX_Province}
      ,F_WX_City = {0:F_WX_City}
      ,F_WX_Language = {0:F_WX_Language}
      ,F_WX_Sex = {0:F_WX_Sex}
      ,F_WX_Subscribe = {0:F_WX_Subscribe}
      ,F_WX_Subscribe_time = {0:F_WX_Subscribe_time}
      ,F_Type = {0:F_Type}
      ,F_BrokerCompanyName = {0:F_BrokerCompanyName}
      ,F_BrokerCompanyKid = {0:F_BrokerCompanyKid}
      ,F_Longitude = {0:F_Longitude}
      ,F_Latitude = {0:F_Latitude}
      ,F_LastLocationTime = {0:F_LastLocationTime}
      ,F_FirstAgreementTime = {0:F_FirstAgreementTime}
      ,F_LastAgreementTime = {0:F_LastAgreementTime}
      ,F_BusinessCardUrl = {0:F_BusinessCardUrl}
      ,F_TagIsSuperAuth = {0:F_TagIsSuperAuth}
      ,F_TagIsSuperAuthTime = {0:F_TagIsSuperAuthTime}
      ,F_TagYear = {0:F_TagYear}
      ,F_TagProgramCount = {0:F_TagProgramCount}
      ,F_TagBusinessCount = {0:F_TagBusinessCount}
      ,F_TagSpareAvgMoney = {0:F_TagSpareAvgMoney}
      ,F_TagCommentCount = {0:F_TagCommentCount}
      ,F_Tag = {0:F_Tag}
      ,F_Remark = {0:F_Remark}
      ,F_CheckInTime = {0:F_CheckInTime}
      ,F_QrCode = {0:F_QrCode}
      ,F_Ex = {0:F_Ex}
      ,F_DeviceID = {0:F_DeviceID}
      ,F_AppVersion = {0:F_AppVersion}
      ,F_HxUuid = {0:F_HxUuid}
      ,F_CardUrl = {0:F_CardUrl}
      ,F_CardCheckAdminKid = {0:F_CardCheckAdminKid}
      ,F_CardCheckTime = {0:F_CardCheckTime}
      ,AnyangKid = {0:AnyangKid}
      ,F_CardCheckStatus = {0:F_CardCheckStatus}
      ,F_StarStatus = {0:F_StarStatus}
 WHERE Kid = {0:Kid}
";
                #endregion
                using (var db3 = DataBaseContext.GetDataBase("default"))
                {
                    for (int i = 0; i < updateList.Count; i++)
                    {
                        db3.Sql(sql2, updateList[i]).ExecuteNonQuery();
                    }
                }
            }
        }

        public static void SyncDataValue_Broker_Part()
        {
            string sql = @"SELECT * FROM t_broker where F_EditTime>{0}";
            List<T_Broker> mySqlList = new List<T_Broker>();
            List<T_Broker> sqlServerList = new List<T_Broker>();
            List<T_Broker> updateList = new List<T_Broker>();
            using (var db2 = DataBaseContext.GetDataBase("sqldefault"))
            {
                sqlServerList = db2.Sql(sql, DateTime.Now.Date.AddDays(-1)).ToList<T_Broker>().Result;
            }
            if (sqlServerList.Count == 0)
            {
                return;
            }
            for (int i = 0; i < sqlServerList.Count; i++)
            {
                updateList.Add(sqlServerList[i]);
            }

            if (updateList.Count > 0)
            {
                #region 更新经纪人数据
                string sql2 = @"
UPDATE T_Broker
   SET F_ProvinceKid = {0:F_ProvinceKid}
      ,F_CityKid = {0:F_CityKid}
      ,F_LocationCityKid = {0:F_LocationCityKid}
      ,F_Title = {0:F_Title}
      ,F_Phone = {0:F_Phone}
      ,F_PassWord = {0:F_PassWord}
      ,F_IDNumber = {0:F_IDNumber}
      ,F_AddTime = {0:F_AddTime}
      ,F_EditTime = {0:F_EditTime}
      ,F_WeiXinID = {0:F_WeiXinID}
      ,F_PicUrl = {0:F_PicUrl}
      ,F_Status = {0:F_Status}
      ,F_AuthTime = {0:F_AuthTime}
      ,F_PassTime = {0:F_PassTime}
      ,F_RealTime = {0:F_RealTime}
      ,F_LastActiveTime = {0:F_LastActiveTime}
      ,F_Bank = {0:F_Bank}
      ,F_BankAccount = {0:F_BankAccount}
      ,F_RecommendPhone = {0:F_RecommendPhone}
      ,F_WX_Nickname = {0:F_WX_Nickname}
      ,F_WX_Country = {0:F_WX_Country}
      ,F_WX_Province = {0:F_WX_Province}
      ,F_WX_City = {0:F_WX_City}
      ,F_WX_Language = {0:F_WX_Language}
      ,F_WX_Sex = {0:F_WX_Sex}
      ,F_WX_Subscribe = {0:F_WX_Subscribe}
      ,F_WX_Subscribe_time = {0:F_WX_Subscribe_time}
      ,F_Type = {0:F_Type}
      ,F_BrokerCompanyName = {0:F_BrokerCompanyName}
      ,F_BrokerCompanyKid = {0:F_BrokerCompanyKid}
      ,F_Longitude = {0:F_Longitude}
      ,F_Latitude = {0:F_Latitude}
      ,F_LastLocationTime = {0:F_LastLocationTime}
      ,F_FirstAgreementTime = {0:F_FirstAgreementTime}
      ,F_LastAgreementTime = {0:F_LastAgreementTime}
      ,F_BusinessCardUrl = {0:F_BusinessCardUrl}
      ,F_TagIsSuperAuth = {0:F_TagIsSuperAuth}
      ,F_TagIsSuperAuthTime = {0:F_TagIsSuperAuthTime}
      ,F_TagYear = {0:F_TagYear}
      ,F_TagProgramCount = {0:F_TagProgramCount}
      ,F_TagBusinessCount = {0:F_TagBusinessCount}
      ,F_TagSpareAvgMoney = {0:F_TagSpareAvgMoney}
      ,F_TagCommentCount = {0:F_TagCommentCount}
      ,F_Tag = {0:F_Tag}
      ,F_Remark = {0:F_Remark}
      ,F_CheckInTime = {0:F_CheckInTime}
      ,F_QrCode = {0:F_QrCode}
      ,F_Ex = {0:F_Ex}
      ,F_DeviceID = {0:F_DeviceID}
      ,F_AppVersion = {0:F_AppVersion}
      ,F_HxUuid = {0:F_HxUuid}
      ,F_CardUrl = {0:F_CardUrl}
      ,F_CardCheckAdminKid = {0:F_CardCheckAdminKid}
      ,F_CardCheckTime = {0:F_CardCheckTime}
      ,AnyangKid = {0:AnyangKid}
      ,F_CardCheckStatus = {0:F_CardCheckStatus}
      ,F_StarStatus = {0:F_StarStatus}
 WHERE Kid = {0:Kid}
";
                #endregion
                using (var db3 = DataBaseContext.GetDataBase("default"))
                {
                    for (int i = 0; i < updateList.Count; i++)
                    {
                        db3.Sql(sql2, updateList[i]).ExecuteNonQuery();
                    }
                }
            }
        }

        public static void SyncDataCount_Admin()
        {
            //先同步数据量
            string sql = @"SELECT COUNT(0) FROM S_Admin";
            using (var db = DataBaseContext.GetDataBase("default"))
            {
                List<S_Admin> newList = new List<S_Admin>();
                var count_MySql = db.Sql(sql).ExecuteScalar().Result;
                using (var db2 = DataBaseContext.GetDataBase("sqldefault"))
                {
                    var count_SqlServer = db2.Sql(sql).ExecuteScalar().Result;
                    int countM = Convert3.To<int>(count_MySql);
                    int countS = Convert3.To<int>(count_SqlServer);
                    if (countS > countM)
                    {
                        string sql2 = @"select max(kid) from S_Admin";
                        int maxId_MySql = Convert3.To<int>(db.Sql(sql2).ExecuteScalar().Result);
                        string sql3 = @"select * from S_Admin where kid>{0}";
                        newList = db2.Sql(sql3, maxId_MySql).ToList<S_Admin>().Result;
                    }
                    #region 插入Admin表
                    string sql4 = @"
INSERT INTO S_Admin
           (Kid
           ,F_RoleID
           ,F_LoginName
           ,F_Password
           ,F_Number
           ,F_Email
           ,F_Sex
           ,F_RealName
           ,F_PicUrl
           ,F_QQ
           ,F_MSN
           ,F_CellPhone
           ,F_OfficePhone
           ,F_HomePhone
           ,F_Remark
           ,F_LastLoginTime
           ,F_LastLoginIP
           ,F_ThisLoginTime
           ,F_ThisLoginIP
           ,F_LoginCount
           ,F_DeviceID
           ,F_AppVersion
           ,F_AppBrokerVersion
           ,F_IsDefine
           ,F_DefineLength
           ,F_DefineTime
           ,F_Enabled
           ,F_AddTime
           ,F_AddAdminKid
           ,F_EditTime
           ,F_EditAdminKid
           ,F_BeginTime
           ,F_EndTime
           ,F_IsReceiveEmail
           ,F_Type
           ,X_Type
           ,X_BrokerCompanyKid
           ,X_FrameworkKid
           ,F_IsOpenDefine)
     VALUES
           ({0:Kid}
           ,{0:F_RoleID}
           ,{0:F_LoginName}
           ,{0:F_Password}
           ,{0:F_Number}
           ,{0:F_Email}
           ,{0:F_Sex}
           ,{0:F_RealName}
           ,{0:F_PicUrl}
           ,{0:F_QQ}
           ,{0:F_MSN}
           ,{0:F_CellPhone}
           ,{0:F_OfficePhone}
           ,{0:F_HomePhone}
           ,{0:F_Remark}
           ,{0:F_LastLoginTime}
           ,{0:F_LastLoginIP}
           ,{0:F_ThisLoginTime}
           ,{0:F_ThisLoginIP}
           ,{0:F_LoginCount}
           ,{0:F_DeviceID}
           ,{0:F_AppVersion}
           ,{0:F_AppBrokerVersion}
           ,{0:F_IsDefine}
           ,{0:F_DefineLength}
           ,{0:F_DefineTime}
           ,{0:F_Enabled}
           ,{0:F_AddTime}
           ,{0:F_AddAdminKid}
           ,{0:F_EditTime}
           ,{0:F_EditAdminKid}
           ,{0:F_BeginTime}
           ,{0:F_EndTime}
           ,{0:F_IsReceiveEmail}
           ,{0:F_Type}
           ,{0:X_Type}
           ,{0:X_BrokerCompanyKid}
           ,{0:X_FrameworkKid}
           ,{0:F_IsOpenDefine})
";
                    #endregion
                    for (int i = 0; i < newList.Count; i++)
                    {
                        db.Sql(sql4, newList[i]).ExecuteNonQuery();
                    }
                }
            }
        }

        public static void SyncDataValue_Admin()
        {
            string sql = @"SELECT * FROM S_Admin";
            List<S_Admin> mySqlList = new List<S_Admin>();
            List<S_Admin> sqlServerList = new List<S_Admin>();
            List<S_Admin> updateList = new List<S_Admin>();
            using (var db = DataBaseContext.GetDataBase("default"))
            {
                mySqlList = db.Sql(sql).ToList<S_Admin>().Result;
            }
            using (var db2 = DataBaseContext.GetDataBase("sqldefault"))
            {
                sqlServerList = db2.Sql(sql).ToList<S_Admin>().Result;
            }
            if (mySqlList.Count != sqlServerList.Count)
            {
                return;
            }
            for (int i = 0; i < sqlServerList.Count; i++)
            {
                if (!sqlServerList[i].Equals(mySqlList[i]))
                {
                    updateList.Add(sqlServerList[i]);
                }
            }

            if (updateList.Count > 0)
            {
                #region 更新Admin数据
                string sql2 = @"
UPDATE S_Admin
   SET F_RoleID = {0:F_RoleID}
      ,F_LoginName = {0:F_LoginName}
      ,F_Password = {0:F_Password}
      ,F_Number = {0:F_Number}
      ,F_Email = {0:F_Email}
      ,F_Sex = {0:F_Sex}
      ,F_RealName = {0:F_RealName}
      ,F_PicUrl = {0:F_PicUrl}
      ,F_QQ = {0:F_QQ}
      ,F_MSN = {0:F_MSN}
      ,F_CellPhone = {0:F_CellPhone}
      ,F_OfficePhone = {0:F_OfficePhone}
      ,F_HomePhone = {0:F_HomePhone}
      ,F_Remark = {0:F_Remark}
      ,F_LastLoginTime = {0:F_LastLoginTime}
      ,F_LastLoginIP = {0:F_LastLoginIP}
      ,F_ThisLoginTime = {0:F_ThisLoginTime}
      ,F_ThisLoginIP = {0:F_ThisLoginIP}
      ,F_LoginCount = {0:F_LoginCount}
      ,F_DeviceID = {0:F_DeviceID}
      ,F_AppVersion = {0:F_AppVersion}
      ,F_AppBrokerVersion = {0:F_AppBrokerVersion}
      ,F_IsDefine = {0:F_IsDefine}
      ,F_DefineLength = {0:F_DefineLength}
      ,F_DefineTime = {0:F_DefineTime}
      ,F_Enabled = {0:F_Enabled}
      ,F_AddTime = {0:F_AddTime}
      ,F_AddAdminKid = {0:F_AddAdminKid}
      ,F_EditTime = {0:F_EditTime}
      ,F_EditAdminKid = {0:F_EditAdminKid}
      ,F_BeginTime = {0:F_BeginTime}
      ,F_EndTime = {0:F_EndTime}
      ,F_IsReceiveEmail = {0:F_IsReceiveEmail}
      ,F_Type = {0:F_Type}
      ,X_Type = {0:X_Type}
      ,X_BrokerCompanyKid = {0:X_BrokerCompanyKid}
      ,X_FrameworkKid = {0:X_FrameworkKid}
      ,F_IsOpenDefine = {0:F_IsOpenDefine}
 WHERE Kid = {0:Kid}
";
                #endregion
                using (var db3 = DataBaseContext.GetDataBase("default"))
                {
                    for (int i = 0; i < updateList.Count; i++)
                    {
                        db3.Sql(sql2, updateList[i]).ExecuteNonQuery();
                    }
                }
            }
        }

        public static void SyncDataValue_Admin_Part()
        {
            string sql = @"SELECT * FROM S_Admin where F_EditTime>{0}";
            List<S_Admin> mySqlList = new List<S_Admin>();
            List<S_Admin> sqlServerList = new List<S_Admin>();
            List<S_Admin> updateList = new List<S_Admin>();
            using (var db2 = DataBaseContext.GetDataBase("sqldefault"))
            {
                sqlServerList = db2.Sql(sql,DateTime.Now.Date.AddDays(-1)).ToList<S_Admin>().Result;
            }
            if (sqlServerList.Count == 0)
            {
                return;
            }
            for (int i = 0; i < sqlServerList.Count; i++)
            {
                updateList.Add(sqlServerList[i]);
            }

            if (updateList.Count > 0)
            {
                #region 更新Admin数据
                string sql2 = @"
UPDATE S_Admin
   SET F_RoleID = {0:F_RoleID}
      ,F_LoginName = {0:F_LoginName}
      ,F_Password = {0:F_Password}
      ,F_Number = {0:F_Number}
      ,F_Email = {0:F_Email}
      ,F_Sex = {0:F_Sex}
      ,F_RealName = {0:F_RealName}
      ,F_PicUrl = {0:F_PicUrl}
      ,F_QQ = {0:F_QQ}
      ,F_MSN = {0:F_MSN}
      ,F_CellPhone = {0:F_CellPhone}
      ,F_OfficePhone = {0:F_OfficePhone}
      ,F_HomePhone = {0:F_HomePhone}
      ,F_Remark = {0:F_Remark}
      ,F_LastLoginTime = {0:F_LastLoginTime}
      ,F_LastLoginIP = {0:F_LastLoginIP}
      ,F_ThisLoginTime = {0:F_ThisLoginTime}
      ,F_ThisLoginIP = {0:F_ThisLoginIP}
      ,F_LoginCount = {0:F_LoginCount}
      ,F_DeviceID = {0:F_DeviceID}
      ,F_AppVersion = {0:F_AppVersion}
      ,F_AppBrokerVersion = {0:F_AppBrokerVersion}
      ,F_IsDefine = {0:F_IsDefine}
      ,F_DefineLength = {0:F_DefineLength}
      ,F_DefineTime = {0:F_DefineTime}
      ,F_Enabled = {0:F_Enabled}
      ,F_AddTime = {0:F_AddTime}
      ,F_AddAdminKid = {0:F_AddAdminKid}
      ,F_EditTime = {0:F_EditTime}
      ,F_EditAdminKid = {0:F_EditAdminKid}
      ,F_BeginTime = {0:F_BeginTime}
      ,F_EndTime = {0:F_EndTime}
      ,F_IsReceiveEmail = {0:F_IsReceiveEmail}
      ,F_Type = {0:F_Type}
      ,X_Type = {0:X_Type}
      ,X_BrokerCompanyKid = {0:X_BrokerCompanyKid}
      ,X_FrameworkKid = {0:X_FrameworkKid}
      ,F_IsOpenDefine = {0:F_IsOpenDefine}
 WHERE Kid = {0:Kid}
";
                #endregion
                using (var db3 = DataBaseContext.GetDataBase("default"))
                {
                    for (int i = 0; i < updateList.Count; i++)
                    {
                        db3.Sql(sql2, updateList[i]).ExecuteNonQuery();
                    }
                }
            }
        }

        public static void SyncDataCount_PointRule()
        {
            //先同步数据量
            string sql = @"SELECT COUNT(0) FROM T_PointRule";
            using (var db = DataBaseContext.GetDataBase("default"))
            {
                List<T_PointRule> newList = new List<T_PointRule>();
                var count_MySql = db.Sql(sql).ExecuteScalar().Result;
                using (var db2 = DataBaseContext.GetDataBase("sqldefault"))
                {
                    var count_SqlServer = db2.Sql(sql).ExecuteScalar().Result;
                    int countM = Convert3.To<int>(count_MySql);
                    int countS = Convert3.To<int>(count_SqlServer);
                    if (countS > countM)
                    {
                        string sql2 = @"select max(kid) from T_PointRule";
                        int maxId_MySql = Convert3.To<int>(db.Sql(sql2).ExecuteScalar().Result);
                        string sql3 = @"select * from T_PointRule where kid>{0}";
                        newList = db2.Sql(sql3, maxId_MySql).ToList<T_PointRule>().Result;
                    }
                    #region 插入Admin表
                    string sql4 = @"
                        INSERT INTO T_PointRule
                                   (F_PointCode
                                   ,F_PointAction
                                   ,F_Point
                                   ,F_Status
                                   ,F_Sort
                                   ,F_AddTime
                                   ,F_EditTime
                                   ,F_AddAdminKid
                                   ,F_EditAdminKid)
                             VALUES
                                   ({0:F_PointCode}
                                   ,{0:F_PointAction}
                                   ,{0:F_Point}
                                   ,{0:F_Status}
                                   ,{0:F_Sort}
                                   ,{0:F_AddTime}
                                   ,{0:F_EditTime}
                                   ,{0:F_AddAdminKid}
                                   ,{0:F_EditAdminKid})
                        ";
                    #endregion
                    for (int i = 0; i < newList.Count; i++)
                    {
                        db.Sql(sql4, newList[i]).ExecuteNonQuery();
                    }
                }
            }
        }

        public static void SyncDataValue_PointRule_Part()
        {
            string sql = @"SELECT * FROM T_PointRule where F_EditTime>{0}";
            List<T_PointRule> mySqlList = new List<T_PointRule>();
            List<T_PointRule> sqlServerList = new List<T_PointRule>();
            List<T_PointRule> updateList = new List<T_PointRule>();
            using (var db2 = DataBaseContext.GetDataBase("sqldefault"))
            {
                sqlServerList = db2.Sql(sql, DateTime.Now.Date.AddDays(-1)).ToList<T_PointRule>().Result;
            }
            if (sqlServerList.Count == 0)
            {
                return;
            }
            for (int i = 0; i < sqlServerList.Count; i++)
            {
                updateList.Add(sqlServerList[i]);
            }

            if (updateList.Count > 0)
            {
                #region 更新Admin数据
                string sql2 = @"
                UPDATE T_PointRule
                   SET F_PointCode = {0:F_PointCode}
                      ,F_PointAction = {0:F_PointAction}
                      ,F_Point = {0:F_Point}
                      ,F_Status = {0:F_Status}
                      ,F_Sort = {0:F_Sort}
                      ,F_AddTime = {0:F_AddTime}
                      ,F_EditTime = {0:F_EditTime}
                      ,F_AddAdminKid = {0:F_AddAdminKid}
                      ,F_EditAdminKid = {0:F_EditAdminKid}
                 WHERE Kid={0:Kid}
                ";
                #endregion
                using (var db3 = DataBaseContext.GetDataBase("default"))
                {
                    for (int i = 0; i < updateList.Count; i++)
                    {
                        db3.Sql(sql2, updateList[i]).ExecuteNonQuery();
                    }
                }
            }
        }

    }
}
